//
//  DialogVC.swift
//  PopupDemo
//
//  Created by zhuxuhong on 2020/11/1.
//

import BWListKit
import BWUIKit

public class DialogVC: PopupVC {
    public var data: Dialog!
    
    public lazy var listView: CollectionView = {
        let v = CollectionView()
        v.backgroundColor = Dialog.appearance.contentView.backgroundColor
        v.layer.cornerRadius = Dialog.appearance.contentView.cornerRadius
        return v
    }()
    
    public lazy var listAdapter: BWListAdapter! = {
        return BWListAdapter(collectionView: listView)
    }()
    
    public init(_ data: Dialog) {
        super.init(nibName: nil, bundle: nil)
        
        self.data = data
        panelView = listView
    }
    
    public required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    public override func viewDidLoad() {
        setupPanelSize()
        
        super.viewDidLoad()
        
        listAdapter.data = listAdapterData
    }
    
    private func setupPanelSize() {
        let W = Dialog.appearance.contentView.width
        let H = Dialog.appearance.contentView.maxHeight
        let h = listAdapterData.calculatedHeight(withCollectionViewWidth: W)
        panelSize = .init(width: W, height: min(H, h))
    }
    
    private lazy var listAdapterData: BWListData = {
        guard let data = data else {
            return .init()
        }
        
        let separatorCellRID = "\(DialogSeparatorCell.self)"
        let textCellRID = "\(DialogTextCell.self)"
        let actionCellRID = "\(DialogActionCell.self)"
        let registers: [BWListRegister] = [
            .init(style: .cell, class: DialogTextCell.self),
            .init(style: .cell, class: DialogActionCell.self),
            .init(style: .cell, class: DialogSeparatorCell.self)
        ]
        
        var sections: [BWListSection] = []
        
        /// 文字区
        var textItems: [BWListItem] = []
        
        let addTextItem: (Dialog.Text?)->Void = {
            guard let text = $0 else {
                return
            }
            let w: CGFloat = Dialog.appearance.contentView.width - Dialog.appearance.text.areaInsets.left - Dialog.appearance.text.areaInsets.right
            let h = DialogTextCell.calculatedHeight(width: w, data: text)
            textItems.append(.init(reuseId: textCellRID, width: w, height: h, data: text))
        }
        addTextItem(data.title)
        addTextItem(data.message)
        if !textItems.isEmpty {
            sections.append(.init(layout: .init(insets: Dialog.appearance.text.areaInsets, minimumLineSpacing: Dialog.appearance.text.areaSpacing), items: textItems))
        }
        
        /// 按钮区
        var actionItems: [BWListItem] = []
        
        let actions = data.actions
        if !actions.isEmpty {
            let count = min(CGFloat(min(3, Dialog.appearance.action.maxCountOneLine)), CGFloat(actions.count))
            let onlyOneLineActions = actions.count <= Dialog.appearance.action.maxCountOneLine
            
            let w = onlyOneLineActions ? (Dialog.appearance.contentView.width - (count-1)*Dialog.appearance.separator.width)/count : Dialog.appearance.contentView.width
            
            let tappedAction: (Dialog.Action)->BWListItemAction =
            { action in
                return .init
                { [weak self] _ in
                    guard action.closeable else {
                        action.handler?()
                        return
                    }
                    self?.close {
                        action.handler?()
                    }
                }
            }
            
            let separatorItem: (_ w: CGFloat, _ h: CGFloat)->BWListItem = {
                .init(reuseId: separatorCellRID, width: $0, height: $1)
            }
            if !textItems.isEmpty {
                actionItems = [separatorItem(Dialog.appearance.contentView.width, Dialog.appearance.separator.width)]
            }
            
            let sw = onlyOneLineActions ? Dialog.appearance.separator.width : Dialog.appearance.contentView.width
            let sh = onlyOneLineActions ? Dialog.appearance.action.height : Dialog.appearance.separator.width
            actions.enumerated().forEach { (idx, action) in
                if idx != 0 {
                    actionItems.append(separatorItem(sw, sh))
                }
                actionItems.append(.init(reuseId: actionCellRID, width: w, height: Dialog.appearance.action.height, data: action, action: tappedAction(action)))
            }
        }
        
        if !actionItems.isEmpty {
            sections += [
                .init(items: actionItems)
            ]
        }
        return .init(registers: registers, sections: sections)
    }()
    
}

